<template>
  <div id="gift">  
  
  <el-form ref="form"  label-width="0px">  
    <el-row>

        <el-col :span="24" style="text-align: right;">
           <el-button type="primary" class="custom_color_button" style="margin-bottom:10px;" @click="gm_but()">{{ variable_name1 }}转化</el-button>      
           <el-button type="primary" class="custom_color_button" style="margin-bottom:10px;" @click="jd_but()">{{ variable_name2 }}转化</el-button>     
           <el-button type="primary" class="custom_color_button" style="margin-bottom:10px;" @click="add_but()">新增</el-button>           
        </el-col>
   
  </el-row>
  </el-form >

   <!-- 渲染数据list -->
   <gift-list :datalist="datalist" @up_list_fun="list_fun"> </gift-list>

      
   <div style="margin-top:20px;text-align: center;">
      <el-pagination
        background
        @current-change="handleCurrentChange"
        :page-size="10"  
        layout="prev, pager, next, jumper"
        :total="count">
      </el-pagination>
    </div>

      <!-- 添加礼物 -->
    <el-dialog title="添加礼物" :visible.sync="add_mtk"  width="500px">
        <div style="width:100%;"> 


        <el-form ref="form"   label-width="70px" style="width:100%;">
    
             <el-form-item label="名称">
                    <el-input    v-model="mtk_form.title" placeholder="名称"></el-input>
              </el-form-item> 
             <el-form-item label="类型">
                <el-select  v-model="mtk_form.select_num" placeholder="请选择" style="width:100%;">
                    <el-option  v-for="(item,index) in select_data" :key="index" :label="item.name" :value="item.id"></el-option>
                </el-select>
             </el-form-item>

              <el-form-item label="消耗">
                    <el-input    v-model="mtk_form.number" placeholder="消耗"></el-input>
              </el-form-item> 

               <el-row style="width:100%;">
                     <el-col :span="12">
                        <el-form-item label="图片">
                            <el-upload 
                                  action="uploadUrl"  
                                  :show-file-list="false"   
                                  :before-upload="beforeupload"
                                  :http-request="ImgUploadSectionFile">
                                  <div class="avatar-uploader">
                                      <img v-if="pic_form.head_img"  :src="pic_form.head_img" class="avatar">
                                      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                  </div>
                            </el-upload>
                         </el-form-item>       
                     </el-col> 

                </el-row>                          
            </el-form>

            <div style="text-align: center;width:50%;margin:auto;">
                <el-button type="primary" class="custom_color_button" style="width:80%;"  @click="add_but_qr">确认</el-button>
            </div>

        </div> 
    </el-dialog>


      <!-- 金豆转积分 -->
    <el-dialog :title="variable_name2 + '转积分比例'" :visible.sync="jd_mtk"  width="400px">
        <div style="width:100%;"> 


        <el-form ref="form"   label-width="70px" style="width:100%;">
    
             <el-form-item label="比例">
                    <el-input    v-model="jdmtk_form.number" placeholder="比例"></el-input>
              </el-form-item> 
                                  
            </el-form>

            <div style="text-align: center;width:50%;margin:auto;">
                <el-button type="primary" class="custom_color_button" style="width:80%;"  @click="jd_but_qr">确认</el-button>
            </div>

        </div> 
    </el-dialog>

      <!-- 果米转积分 -->
    <el-dialog :title="variable_name1 + '转积分比例'" :visible.sync="gm_mtk"  width="400px">
        <div style="width:100%;"> 


        <el-form ref="form"   label-width="70px" style="width:100%;">
    
             <el-form-item label="比例">
                    <el-input    v-model="gmmtk_form.number" placeholder="比例"></el-input>
              </el-form-item> 
                                  
            </el-form>

            <div style="text-align: center;width:50%;margin:auto;">
                <el-button type="primary" class="custom_color_button" style="width:80%;"  @click="gm_but_qr">确认</el-button>
            </div>

        </div> 
    </el-dialog>



   
      
  </div>
</template>

<script>
import GiftList from '@/view/Live/GiftList'
export default {
  name: 'gift',
  data () {
    return {
        datalist:[],
        page:1,
        count:0,
        add_mtk:false,
        mtk_form:{
            title:'',
            number:'',
            select_num:1,
        },
         pic_form:{
           head_img:'',
           upfile:'',
        },   
        select_data:[{"id":1,"name":this.variable_name1},{"id":2,"name":this.variable_name2}],
        jd_mtk:false,
        jdmtk_form:{
          number:''
        },
        gm_mtk:false,
        gmmtk_form:{
          number:''
        },
    }
  },
 components:{
    GiftList
  },
  created(){
      this.list_fun()
      this.bl_fun()
  },
  mounted (){
    
  },
  methods: { 
     bl_fun(){
       this.post("/live_gift/exchangeScale", {
              
	        }).then(res => {
            this.jdmtk_form.number = res.result.gdb_to_score_scale
            this.gmmtk_form.number = res.result.kmb_to_score_scale
         })
     },
      list_fun(){
           this.post("/live_gift/listOfData", {
              page:this.page,
	        }).then(res => {
            this.count = res.result.count
            this.datalist = res.result.list
            //console.log(this.datalist)
         })
      },
      add_but(){
           this.add_mtk = true
      },
      add_but_qr(){
            this.post("/live_gift/create", {
                title:this.mtk_form.title,
                icon:this.pic_form.upfile,
                need_price:this.mtk_form.number,
                need_type:this.mtk_form.select_num,
              }).then(res => {
                if(res.error_code == 0){
                    this.open1(res.result,'success')  //提示框
                    this.list_fun()
                    this.add_mtk = false
                    this.mtk_form.title=''
                    this.pic_form.upfile = ''
                    this.pic_form.head_img = ''
                    this.mtk_form.number = ''
                    this.mtk_form.select_num = 1
                }else {
                   this.open1(res.message,'warning')
                }

            })
      },
      jd_but(){
           this.jd_mtk = true
      },
      jd_but_qr(){
             this.post("/live_gift/setExchangeGdbScale", {
                gdb_to_score_scale:this.jdmtk_form.number
              }).then(res => {
                if(res.error_code == 0){
                    this.open1(res.result,'success')  //提示框
                    this.jd_mtk = false
                    this.bl_fun()
                }else {
                   this.open1(res.message,'warning')
                }

            })
      },
      gm_but(){
           this.gm_mtk = true
      },
      gm_but_qr(){
             this.post("/live_gift/setExchangeKmbScale", {
                kmb_to_score_scale:this.gmmtk_form.number
              }).then(res => {
                if(res.error_code == 0){
                    this.open1(res.result,'success')  //提示框
                    this.gm_mtk = false
                    this.bl_fun()
                }else {
                   this.open1(res.message,'warning')
                }

            })
      },
     beforeupload(file){   //重新写入上穿 需要点击确认上传与表单一起提交
           if(this.upimglx(file) == false){  //判断是否为fales
              this.upimglxtitle()  //执行弹出错误             
              return false
            }  
            var windowURL = window.URL || window.webkitURL;               
            this.pic_form.head_img=windowURL.createObjectURL(file);             
            
            this.upimgpost("/image/uploadMultiple",file).then(res => {
              console.log(res.result.url )
               this.pic_form.upfile = res.result.url    
            })

            return false;
     },
      ImgUploadSectionFile(param){//图片上传    
      },
     handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
      },
      handleCurrentChange(val) {
        this.page = val
        this.list_fun()
        console.log(`当前页: ${val}`);
      }
  }
}
</script>


<style scoped>
.avatar-uploader  {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 150px;
    height: 150px;
    line-height: 150px;
    text-align: center;
  }
  .avatar {
    width: 150px;
    height: 150px;
    display: block;
  }
</style>
